/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hydms.illegalregistration.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.hydms.healthcheck.entity.DmsHealthCheck;
import com.thinkgem.jeesite.modules.hydms.illegalregistration.entity.DmsIllegalRegistration;
import com.thinkgem.jeesite.modules.hydms.illegalregistration.service.DmsIllegalRegistrationService;
import com.thinkgem.jeesite.modules.hydms.students.entity.DmsStudents;
import com.thinkgem.jeesite.modules.hydms.students.service.DmsStudentsService;

/**
 * 违规登记Controller
 * @author 刘健
 * @version 2017-04-05
 */
@Controller
@RequestMapping(value = "${adminPath}/illegalregistration/dmsIllegalRegistration")
public class DmsIllegalRegistrationController extends BaseController {
	@Autowired
	private DmsStudentsService dmsStudentsService;
	@Autowired
	private DmsIllegalRegistrationService dmsIllegalRegistrationService;
	
	@ModelAttribute
	public DmsIllegalRegistration get(@RequestParam(required=false) String id) {
		DmsIllegalRegistration entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = dmsIllegalRegistrationService.get(id);
		}
		if (entity == null){
			entity = new DmsIllegalRegistration();
		}
		return entity;
	}
	
	
	/**
	 * 导出用户数据
	 * @param DmsIllegalRegistration
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("illegalregistration:dmsIllegalRegistration:view")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(DmsIllegalRegistration dmsIllegalRegistration, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "违规信息"+DateUtils.getDate("yyyyMMdd")+".xlsx";
            Page<DmsIllegalRegistration> page = dmsIllegalRegistrationService.findPage(new Page<DmsIllegalRegistration>(request, response, -1), dmsIllegalRegistration);
    		new ExportExcel("违规信息", DmsIllegalRegistration.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "违规信息导出失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/illegalregistration/dmsIllegalRegistration/list?repage";
    }
	
	
	/**
	 * 根据学号查找学生姓名
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("illegalregistration:dmsIllegalRegistration:view")
	@RequestMapping(value = "checkName")
	public void checkName(DmsIllegalRegistration dmsIllegalRegistration,String idStuNum, HttpServletRequest request, HttpServletResponse response, Model model) {
		DmsStudents students=new DmsStudents();
		students.setStudentNumber(idStuNum);
		List<DmsStudents> list=dmsStudentsService.findList(students);
		 try {
			 if(list.size()<1){
				response.getWriter().print("null");
		    }else {
				for(DmsStudents stu:list){
					response.getWriter().print(stu.getStudentName());
				}
		      }
		       } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			  }
		
	}
	
	/**
	 * 宿舍违规登记列表
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("illegalregistration:dmsIllegalRegistration:view")
	@RequestMapping(value = {"list", ""})
	public String list(DmsIllegalRegistration dmsIllegalRegistration, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DmsIllegalRegistration> page = dmsIllegalRegistrationService.findPage(new Page<DmsIllegalRegistration>(request, response), dmsIllegalRegistration); 
		model.addAttribute("page", page);
		return "hydms/illegalregistration/dmsIllegalRegistrationList";
	}

	/**
	 * 宿舍违规登记添加
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("illegalregistration:dmsIllegalRegistration:view")
	@RequestMapping(value = "form")
	public String form(DmsIllegalRegistration dmsIllegalRegistration, Model model) {
		model.addAttribute("dmsIllegalRegistration", dmsIllegalRegistration);
		return "hydms/illegalregistration/dmsIllegalRegistrationForm";
	}

	/**
	 * save
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("illegalregistration:dmsIllegalRegistration:edit")
	@RequestMapping(value = "save")
	public String save(DmsIllegalRegistration dmsIllegalRegistration, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, dmsIllegalRegistration)){
			return form(dmsIllegalRegistration, model);
		}
		dmsIllegalRegistrationService.save(dmsIllegalRegistration);
		addMessage(redirectAttributes, "保存违规登记成功");
		return "redirect:"+Global.getAdminPath()+"/illegalregistration/dmsIllegalRegistration/?repage";
	}
	
	/**
	 * delete
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("illegalregistration:dmsIllegalRegistration:edit")
	@RequestMapping(value = "delete")
	public String delete(DmsIllegalRegistration dmsIllegalRegistration, RedirectAttributes redirectAttributes) {
		dmsIllegalRegistrationService.delete(dmsIllegalRegistration);
		addMessage(redirectAttributes, "删除违规登记成功");
		return "redirect:"+Global.getAdminPath()+"/illegalregistration/dmsIllegalRegistration/?repage";
	}

}